﻿using Apps.BLL.Report;
using Apps.Common;
using Apps.IBLL.LMS;
using Apps.IBLL.PUB;
using Apps.Models;
using Apps.Web.Core;
using Microsoft.Practices.Unity;
using Microsoft.Reporting.WebForms;
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Apps.Web.Areas.Report.Controllers
{
    public class AssayDayReportController : BaseController
    {
        [Dependency]
        public IPUB_SUPPLIERBLL m_SUPPLIERBLL { get; set; }


        [SupportFilter(ActionName = "Index")]
        public ActionResult Index(string MATERIEL_ID, string TRANSPORT_ID, string STime, string ETime)
        {

            ViewBag.MATERIEL_ID = MATERIEL_ID;

            if (string.IsNullOrWhiteSpace(STime))
            {
                ViewBag.STime = DateTime.Now.ToString("yyyy-MM-01");  //默认为当天
                STime = ViewBag.STime;
            }
            if (string.IsNullOrWhiteSpace(ETime))
            {
                ViewBag.ETime = DateTime.Now.ToString("yyyy-MM-dd");
                ETime = ViewBag.ETime;
            }

            ReportViewer reportViewer = new ReportViewer();
            ViewBag.ReportViewer = reportViewer;
            reportViewer.ShowPrintButton = true;
            reportViewer.SizeToReportContent = true;
            reportViewer.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"\ReportFile\AssayReport\AssayDay_Report_New.rdlc";
            reportViewer.LocalReport.SetParameters(new ReportParameter("TabulationCompany", siteConfig.TabulationCompany));
            reportViewer.LocalReport.DataSources.Clear();
            reportViewer.LocalReport.DataSources.Add(new ReportDataSource { Value = Report_DT(MATERIEL_ID, STime, ETime), Name = "DataSet1" });
            reportViewer.LocalReport.Refresh();

            return View();
        }

        private DataTable Report_DT(string _MATERIEL_ID, string _STime, string _ETime)
        {
            string strConnDB = ConfigurationManager.ConnectionStrings["OracleDbContext"].ToString();
            string _strCmdWhere = " 1 = 1 ";
            if (_MATERIEL_ID != null && _MATERIEL_ID != "-1")
                _strCmdWhere += @" AND AUDIT_ID = '" + _MATERIEL_ID + "' ";

            if (_STime != null && _STime.Length > 0)
                _strCmdWhere += @" AND UPDATE_TIME >= to_date('" + _STime + "','YYYY-MM-DD')";

            if (_ETime != null && _ETime.Length > 0)
                _strCmdWhere += @" AND UPDATE_TIME <= to_date('" + _ETime + "','YYYY-MM-DD')";

            string _strCmd = string.Format(@"SELECT AA.*,NVL(BB.CARCOUNT,0) AS CARCOUNT
                                              FROM V_LMS_REPORT AA
                                            LEFT JOIN (SELECT  BATCH_CODE,COUNT(CAR_ID) AS CARCOUNT FROM V_RC_IN WHERE BATCH_CODE IS NOT NULL GROUP BY BATCH_CODE) BB
                                            ON AA.BATCH_CODE=BB.BATCH_CODE
                                             WHERE  {0}", _strCmdWhere);
            DataTable _dt = new DataTable();
            Oracle.ManagedDataAccess.Client.OracleDataAdapter _orcDA = new Oracle.ManagedDataAccess.Client.OracleDataAdapter(_strCmd, strConnDB);
            _orcDA.Fill(_dt);
            return _dt;
        }
    }
}